home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 1999 November / SGI IRIX 6.5 Applications 1999 November.iso / dev / java_dev.idb / usr / demos / java / java-and-video / video2screen.c.z / video2screen.c
Encoding:
C/C++ Source or Header  |  1998-07-14  |  9.3 KB  |  401 lines

  1. #include <X11/Intrinsic.h>
  2. #include <stdlib.h>
  3.  
  4. #include "MyCanvas.h"
  5. #include "monitor.h"
  6.  
  7.  
  8. #define AWT_LOCK() awt_lock_enter()
  9. #define AWT_UNLOCK() awt_lock_exit()
  10.  
  11.  
  12.  
  13. struct ComponentData {
  14.   Widget      widget;
  15.   int         repaintPending;
  16.   int         x1, y1, x2, y2;
  17. };
  18.  
  19. struct CanvasData {
  20.   struct ComponentData        comp;
  21.   Widget                      shell;
  22.   int                         flags;
  23. };
  24.  
  25. int vtos(Window);
  26.  
  27.  
  28. JNIEXPORT void JNICALL Java_MyCanvas_Video2Screen
  29. (JNIEnv *env, jobject this, jint pData)
  30. {
  31.  
  32.   struct CanvasData *wdata;
  33.   Window w;
  34.   Display *d;
  35.   GC gc;
  36.   XWindowAttributes wattr;
  37.   int x;
  38.  
  39.   wdata = (struct CanvasData*)pData;
  40.   AWT_LOCK();
  41.   x = vtos(XtWindow(wdata->comp.widget));
  42.   AWT_UNLOCK();
  43.  
  44. }
  45.  
  46.  
  47. /*
  48. ** vtos.c - video to screen
  49. */
  50.  
  51. #ident "$Revision: 1.2 $"
  52.  
  53. #include <sys/types.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <getopt.h>
  57. #include <errno.h>
  58. #include <unistd.h>
  59. #include <string.h>
  60. #include <bstring.h>
  61. #include <sys/time.h>
  62. #include <limits.h>
  63. #include <dmedia/dmedia.h>
  64. #include <dmedia/vl.h>
  65. #include <dmedia/dm_buffer.h>
  66. #include <dmedia/dm_params.h>
  67. #include <gl/image.h>
  68.  
  69. #include <X11/X.h>   /* X */
  70. #include <X11/Xlib.h>
  71. #include <X11/Xutil.h>
  72. #include <X11/keysym.h>
  73. #include <Xm/MwmUtil.h>
  74.  
  75. #include <GL/glx.h>  /* OpenGL */
  76. #include <GL/gl.h>
  77.  
  78. int ntoi(char *str, int *val);
  79. void usage(void);
  80. void CreateWindowAndContext(Window *win, GLXContext *ctx, int *visualAttr, 
  81.     char name[], Window parent);
  82.  
  83. char *prognm;
  84.  
  85. int Debug_f = 1;
  86. int Nframes_f = 1;
  87. int Viddev_f = VL_ANY;
  88. int Vidnode_f = VL_ANY;
  89. int mask = VLTransferCompleteMask | VLTransferFailedMask;
  90. Display *dpy;
  91. Window window;
  92. GLXContext        ctx;
  93. int         init_x = 0;
  94. int         init_y = 0;
  95. XVisualInfo *vi;
  96.  
  97. /* smallest single buffered RGBA visual */
  98. int visualAttr[] = {GLX_RGBA, GLX_RED_SIZE, 8, 
  99.             GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8,
  100.             None};
  101.  
  102. int xsize, ysize;
  103.  
  104. #define MAX(a,b)    (((a)>(b))?(a):(b))
  105.  
  106. int
  107. vtos(Window parent)
  108. {
  109.     int    c, errflg = 0;
  110.     VLServer svr;
  111.     VLNode drn;
  112.     VLNode src;
  113.     VLPath path;
  114.     VLControlValue val;
  115.     DMparams * plist;
  116.     VLEvent event;
  117.     int xfersize;
  118.     DMbufferpool pool;
  119.     DMbuffer dmbuffer;
  120.     fd_set fdset;
  121.     int    maxfd;
  122.     int pathfd;
  123.     int xfd;
  124.     int i;
  125.     int nready;
  126.     int done = 0;
  127.     void *addr;
  128.  
  129.  
  130.     prognm = "XApplication";
  131.  
  132.     if( (svr = vlOpenVideo("")) == NULL ) {
  133.     fprintf(stderr,"%s: vlOpenVideo failed\n",prognm);
  134.     return 1;
  135.     }
  136.  
  137.     if( (drn = vlGetNode(svr, VL_DRN, VL_MEM, VL_ANY)) == -1 ) {
  138.     vlPerror("get mem drain node");
  139.     return 1;
  140.     }
  141.  
  142.     if( (src = vlGetNode(svr, VL_SRC, VL_VIDEO, Vidnode_f)) == -1 ) {
  143.     vlPerror("get video source node");
  144.     return 1;
  145.     }
  146.  
  147.     if( (path = vlCreatePath(svr, Viddev_f, src, drn)) == -1 ) {
  148.     vlPerror("get path");
  149.     return 1;
  150.     }
  151.  
  152.     if( vlSetupPaths(svr, (VLPathList)&path, 1, VL_SHARE, VL_SHARE) == -1 ) {
  153.     vlPerror("setup paths");
  154.     return 1;
  155.     }
  156.  
  157.     /* Set packing mode to RGB */
  158.     val.intVal = VL_PACKING_RGB_8;
  159.     vlSetControl(svr, path, drn, VL_PACKING, &val);
  160.  
  161.     val.intVal = VL_FORMAT_RGB;
  162.     vlSetControl(svr, path, drn, VL_FORMAT, &val);
  163.  
  164.     vlGetControl(svr, path, drn, VL_SIZE, &val);
  165.     xsize = val.xyVal.x;
  166.     ysize = val.xyVal.y;
  167.  
  168.     /* set up the OpenGL/X window */
  169.     CreateWindowAndContext(&window, &ctx, visualAttr, "vtos:", parent);
  170.     XSelectInput(dpy, window, ButtonPressMask|KeyPressMask);
  171.     glLoadIdentity();
  172.     glViewport(0, 0, xsize,ysize);
  173.     glOrtho(0, xsize, 0, ysize, -1, 1);
  174.     glRasterPos2f(0, ysize-1);
  175.     glDrawBuffer(GL_FRONT); /* On Vino, we only use the front buffer */
  176.             /* video is upside down to graphics! */
  177.     glPixelZoom(1.0, -1.0);
  178.  
  179.     if( dmParamsCreate(&plist) == DM_FAILURE ) {
  180.     fprintf(stderr, "%s: create params\n", prognm);
  181.     return 1;
  182.     }
  183.  
  184.     xfersize = vlGetTransferSize(svr, path);
  185.  
  186.     if( dmBufferSetPoolDefaults(plist, Nframes_f, xfersize, DM_FALSE, DM_FALSE)
  187.     == DM_FAILURE ) {
  188.     fprintf(stderr, "%s: set pool defs\n",prognm);
  189.     return 1;
  190.     }
  191.  
  192.     if( vlDMPoolGetParams(svr, path, drn, plist) == DM_FAILURE ) {
  193.     fprintf(stderr, "%s: get pool reqs\n",prognm);
  194.     return 1;
  195.     }
  196.  
  197.     /* create the buffer pool */
  198.     if( dmBufferCreatePool(plist, &pool) == DM_FAILURE ) {
  199.     fprintf(stderr, "%s: create pool\n", prognm);
  200.     return 1;
  201.     }
  202.  
  203.     if( vlDMPoolRegister(svr, path, drn, pool) == DM_FAILURE ) {
  204.     fprintf(stderr, "%s: register pool\n", prognm);
  205.     return 1;
  206.     }
  207.  
  208.     if( vlSelectEvents(svr, path, mask) == -1 ) {
  209.     vlPerror("select events");
  210.     return 1;
  211.     }
  212.  
  213.     /* Begin the data transfer */
  214.     if( vlBeginTransfer(svr, path, 0, NULL) == -1 ) {
  215.     vlPerror("begin transfer");
  216.     return 1;
  217.     }
  218.  
  219.     /* spin in the get event loop until we've reached nframes */
  220.     if( vlPathGetFD(svr, path, &pathfd) ) {
  221.     vlPerror("getpathfd");
  222.     return 1;
  223.     }
  224.     xfd = ConnectionNumber(dpy);
  225.     maxfd = MAX(xfd, pathfd) + 1;
  226.  
  227.     while( !done ) {
  228.     FD_ZERO(&fdset);
  229.     FD_SET(pathfd, &fdset);
  230.  
  231.     if( (nready = select(maxfd, &fdset, 0, 0, 0)) == -1 ) {
  232.         perror("select");
  233.         return 1;
  234.     }
  235.  
  236.     if( nready == 0 ) {
  237.         continue;
  238.     }
  239.  
  240.     if( FD_ISSET(xfd, &fdset) ) {
  241.         XEvent event;
  242.         KeySym key;
  243.  
  244.         /* ignore them for now. */
  245.         while( XPending(dpy) ) {
  246.         XNextEvent(dpy, &event);
  247.         switch( event.type ) {
  248.         case ButtonPress:
  249.             break;
  250.         case KeyPress:
  251.             XLookupString(&event.xkey, NULL, 0, &key, NULL);
  252.             switch (key) {
  253.             case XK_Escape:
  254.             case XK_q:
  255.             done = 0;
  256.             break;
  257.             default:
  258.             break;
  259.             }
  260.             break;
  261.         default:
  262.             break;
  263.         }
  264.         }
  265.     }
  266.     if( FD_ISSET(pathfd, &fdset) ) {
  267.         while( vlEventRecv(svr, path, &event) == DM_SUCCESS ) {
  268.         switch( event.reason ) {
  269.         case VLTransferComplete:
  270.             vlEventToDMBuffer(&event, &dmbuffer);
  271.             addr = dmBufferMapData(dmbuffer);
  272.             glDrawPixels(xsize, ysize, GL_ABGR_EXT, GL_UNSIGNED_BYTE, 
  273.             (char *) addr);
  274.             (void)dmBufferFree(dmbuffer);
  275.                 /* dump it to the screen */
  276.             break;
  277.         case VLTransferFailed:
  278.             fprintf(stderr, "Transfer failed!\n");
  279.             done = 1;
  280.             break;
  281.         default:
  282.             if( Debug_f ) {
  283.             fprintf(stderr, "Received unexpect event %s\n", 
  284.                 vlEventToName(event.reason));
  285.             }
  286.             break;
  287.         }
  288.         }
  289.     }
  290.     }
  291.  
  292.     (void)vlEndTransfer(svr, path);
  293.  
  294.     (void)vlDMPoolDeregister(svr, path, src, pool);
  295.  
  296.     (void)vlDestroyPath(svr, path);
  297.  
  298.     (void)vlCloseVideo(svr);
  299.     return 0;
  300. }
  301.  
  302. int
  303. xioerror( Display *dpy )
  304. {
  305.     exit(1); 
  306.     return 0;
  307. }
  308.  
  309. Bool 
  310. waitForMapNotify(Display *dpy, XEvent *ev, XPointer arg)
  311. {
  312.         return (ev->type == MapNotify && ev->xmapping.window == *((Window *) arg));
  313. }
  314.  
  315. /* create and window and opengl context */
  316. void    
  317. CreateWindowAndContext(Window *win, GLXContext *ctx,int *visualAttr,char name[], Window parent)
  318. {
  319.     XSizeHints hints;
  320.     XSetWindowAttributes swa;
  321.     Colormap cmap;
  322.     int isRgba;
  323.     XEvent event;
  324.     XIOErrorHandler  oldXError;
  325.     int i;
  326.  
  327.     hints.min_width = xsize/2;
  328.     hints.max_width = 2*xsize;
  329.     hints.base_width = hints.width = xsize;
  330.     hints.min_height = ysize/2;
  331.     hints.max_height = 2*ysize;
  332.     hints.base_height = hints.height = ysize;
  333.     hints.flags = USSize | PMinSize | PMaxSize;
  334.                 /* If user requested initial window position of 0,0 */
  335.     /* get a connection */
  336.     if(dpy == NULL)
  337.        dpy = XOpenDisplay(NULL);
  338.     if (dpy == NULL) {
  339.         fprintf(stderr, "Can't connect to display `%s'\n",
  340.                 getenv("DISPLAY"));
  341.         exit(EXIT_FAILURE);
  342.     }
  343.     oldXError = XSetIOErrorHandler (xioerror);
  344.     
  345.     /* get an appropriate visual */
  346.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), visualAttr);
  347.     if (vi == NULL) {
  348.         fprintf(stderr, "No matching visual on display `%s'\n",
  349.                 getenv("DISPLAY"));
  350.         exit(EXIT_FAILURE);
  351.     }
  352.     (void) glXGetConfig(dpy, vi, GLX_RGBA, &isRgba);
  353.     /* create a GLX context */
  354.     if ((*ctx = glXCreateContext(dpy, vi, 0, GL_TRUE)) == NULL){
  355.        fprintf(stderr,"Cannot create a context.\n");
  356.        exit(EXIT_FAILURE);
  357.     }
  358.     /* create a colormap (it's empty for rgba visuals) */
  359.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  360.                            vi->visual, isRgba ? AllocNone : AllocAll);
  361.     if (cmap == NULL) {
  362.         fprintf(stderr,"Cannot create a colormap.\n");
  363.         exit(EXIT_FAILURE);
  364.     }
  365.     /* fill the colormap with something simple */
  366.     if (!isRgba) {
  367.         static char *cnames[] = {
  368.             "black","red","green","yellow","blue","magenta","cyan","white"
  369.         };
  370.                         
  371.         for (i = 0; i < sizeof(cnames)/sizeof(cnames[0]); i++) {
  372.             XStoreNamedColor(dpy, cmap, cnames[i], i, DoRed|DoGreen|DoBlue);
  373.         }
  374.     }
  375.  
  376.     /* create a window */
  377.     swa.colormap = cmap;
  378.     swa.border_pixel = 0;
  379.     swa.event_mask = StructureNotifyMask |KeyPressMask | ExposureMask;
  380.  
  381.     *win = XCreateWindow(dpy, parent,
  382.                         init_x, init_y, xsize, ysize,
  383.                         0, vi->depth, InputOutput, vi->visual,
  384.                         CWBorderPixel | CWColormap | CWEventMask, &swa);
  385.  
  386.     if (*win == NULL) {   
  387.         fprintf(stderr,"Cannot create a window.\n");
  388.         exit(EXIT_FAILURE);
  389.     }
  390.     XStoreName(dpy, *win, name);  
  391.     XSetNormalHints(dpy, *win, &hints);
  392.  
  393.     XMapWindow(dpy, *win);
  394.     XIfEvent(dpy, &event, waitForMapNotify, (XPointer) win);
  395.     /* Connect the context to the window */
  396.     if (!glXMakeCurrent(dpy, *win, *ctx)) {
  397.         fprintf(stderr, "Can't make window current to context\n");
  398.         exit(EXIT_FAILURE);
  399.     }
  400. }
  401.